package aop1.aspectjAnnotation;

import java.util.Arrays;

import javax.annotation.PostConstruct;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.Assert;

@Aspect
public class ImprovedLoggingAspect {
	private String beforeMessage;
	private String afterMessage;

	@Around("execution(* aop1.aspectjAnnotation.TestBean.*(..))")
	public Object log(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println(String.format(this.beforeMessage, pjp.getSignature()
				.getName(), Arrays.toString(pjp.getArgs())));
		Object ret = pjp.proceed();
		System.out.println(String.format(this.afterMessage, pjp.getSignature()
				.getName(), Arrays.toString(pjp.getArgs())));
		return ret;
	}

	@PostConstruct
	public void initialize() {
		Assert.notNull(this.beforeMessage, "The [beforeMessage] property of ["
				+ getClass().getName() + "] must be set.");
		Assert.notNull(this.afterMessage, "The [afterMessage] property of ["
				+ getClass().getName() + "] must be set.");
	}

	public void setBeforeMessage(String beforeMessage) {
		this.beforeMessage = beforeMessage;
	}

	public void setAfterMessage(String afterMessage) {
		this.afterMessage = afterMessage;
	}
}
